// 登录函数
function login() {  
    const form = document.querySelector('.login-form');  
    const formData = new FormData(form);  
    const username = formData.get('login-username');  
    const password = formData.get('login-password');  
  
    console.log("尝试登录：用户名：" + username);  
  
    if (!username || !password) {  
        showToast('用户名和密码不能为空');  
        return;  
    }  
  
    if (username.length < 8 || username.length > 30 || password.length < 6 || password.length > 30) {  
        showToast('用户名长度应为8-30个字符，密码长度应为6-30个字符');  
        return;  
    }  
  
    axios.post('/login', {  
        username: username,  
        password: password  
    })  
    .then(response => {  
        // 登录成功  
        if (response.data.success) { 
            showToast(response.data.message);  
            console.log(response);
            
            localStorage.setItem('token', response.data.token);  
            localStorage.setItem('username', response.data.username);  
            setTimeout(() => {  
                location.href = '../Gamecenter.html'; // 跳转到首页  
            }, 1500);  
        } else {  
            showToast(response.data.message);  
        }  
    })  
    .catch(error => {  
        if (error.response && error.response.data) {  
            showToast(error.response.data.message);  
        } else {  
            showToast('登录失败，请稍后再试或检查您的网络连接');  
        }  
    });  
}  